<?php

//include('constants.php');
$host = '192.168.1.10';
//$ports = array();
global $out, $map, $char, $login, $inter, $readme, $file;
$out = '';
$map = '';
$char = '';
$login = '';
$inter = '';
$readme = '';


if(ISSET($_POST['download'])) {
	parsedata();
} else{
	if(ISSET($_POST['lport'])) {
		$host = (string)$_POST['lip'];
		$ports = (string)$_POST['lport'];
		if(strlen($ports) > 1){
			//$out .= checkport($host, $ports);
		}
	}
	if(ISSET($_POST['mport'])) {
		$host = (string)$_POST['mip'];
		$ports = (string)$_POST['mport'];
		if(strlen($ports) > 1){
			$out .= checkport($host, $ports);
		}
	}
	if(ISSET($_POST['cport'])) {
		$host = (string)$_POST['cip'];
		$ports = (string)$_POST['cport'];
		if(strlen($ports) > 1){
			//$out .= checkport($host, $ports);
		}
	}
	echo $out;
}

function parsedata(){
	global $login, $char, $map, $inter, $readme;
	if($_POST['rdio1'] == "Yes"){
		$lserv = true;
	} else{
		$lserv = false;
	}
	$lip = $_POST['lip'];
	$mip = $_POST['mip'];
	$cip = $_POST['cip'];
	if($_POST['rdio2'] == "Yes"){
		$dport = true;
	} else{
		$dport = false;
	}
	$lport = $_POST['lport'];
	$mport = $_POST['mport'];
	$cport = $_POST['cport'];
	$sname = $_POST['sname'];
	$suname = $_POST['suname'];
	$spass = $_POST['spass'];
	
	if($_POST['rdio3'] == "SQL"){
		$stype = true;
	} else{
		$stype = false;
	}
	$sqlhost = $_POST['hostname'];
	$sqlport = $_POST['port'];
	$dbuname = $_POST['username'];
	$dbpass = $_POST['password'];
	$dbname = $_POST['database'];
	
	if($_POST['rdio4'] == "SQL"){
		$dstype = true;
	} else{
		$dstype = false;
	}
	if($_POST['rdio5'] == "SQL"){
		$astype = true;
	} else{
		$astype = false;
	}

	$tmp = "//----------------------------------------------------------".PHP_EOL;
	$tmp.= "// Created using eAthena Online Configuration by znromonk".PHP_EOL;
	$tmp.= "//".PHP_EOL;
	$tmp.= "// eAthena r15091".PHP_EOL;
	$tmp.= "//----------------------------------------------------------".PHP_EOL;
	$tmp.= "// Version 0.1".PHP_EOL;
	$tmp.= "//----------------------------------------------------------".PHP_EOL;
	$tmp.= "".PHP_EOL;
	$tmp.= "".PHP_EOL;
	
	$map.= $tmp;
	$char.= $tmp;
	$inter.= $tmp;
	$login.= $tmp;
	
	// login_athena.conf and char_athena.conf
	if(!$dport){
		changeprts($lport, $mport, $cport);
	}

	// login_athena.conf
	if($astype){
		$login.= "// Which account engine to use.".PHP_EOL;
		$login .= "account.engine: sql".PHP_EOL;
		$login.= "".PHP_EOL;
	} else{
		$login.= "// Which account engine to use.".PHP_EOL;
		$login .= "account.engine: txt".PHP_EOL;
		$login.= "".PHP_EOL;
	}
	
	// inter_athena.conf
	if($stype){
		$inter.= "// Global SQL settings".PHP_EOL;
		$inter.= "sql.db_hostname: ".$sqlhost.PHP_EOL;
		$inter.= "sql.db_port: ".$sqlport.PHP_EOL;
		$inter.= "sql.db_username: ".$dbuname.PHP_EOL;
		$inter.= "sql.db_password: ".$dbpass.PHP_EOL;
		$inter.= "sql.db_database: ".$dbname.PHP_EOL;
		$inter.= "".PHP_EOL;
		
		$inter.= "// MySQL Character SQL server".PHP_EOL;
		$inter.= "char_server_ip: ".$sqlhost.PHP_EOL;
		$inter.= "char_server_port: ".$sqlport.PHP_EOL;
		$inter.= "char_server_id: ".$dbuname.PHP_EOL;
		$inter.= "char_server_pw: ".$dbpass.PHP_EOL;
		$inter.= "char_server_db: ".$dbname.PHP_EOL;
		$inter.= "".PHP_EOL;

		$inter.= "// MySQL Map SQL Server".PHP_EOL;
		$inter.= "map_server_ip: ".$sqlhost.PHP_EOL;
		$inter.= "map_server_port: ".$sqlport.PHP_EOL;
		$inter.= "map_server_id: ".$dbuname.PHP_EOL;
		$inter.= "map_server_pw: ".$dbpass.PHP_EOL;
		$inter.= "map_server_db: ".$dbname.PHP_EOL;
		$inter.= "".PHP_EOL;

		$inter.= "// MySQL Log SQL Database".PHP_EOL;
		$inter.= "log_db_ip: ".$sqlhost.PHP_EOL;
		$inter.= "log_db_port: ".$sqlport.PHP_EOL;
		$inter.= "log_db_id: ".$dbuname.PHP_EOL;
		$inter.= "log_db_pw: ".$dbpass.PHP_EOL;
		$inter.= "log_db_db: ".$dbname.PHP_EOL;
		$inter.= "".PHP_EOL;
	}
	
	if($dstype){
		$inter.= "//Use SQL item_db and mob_db for the map server".PHP_EOL;
		$inter.= "use_sql_db: yes".PHP_EOL;
		$inter.= "".PHP_EOL;
	} else{
		$inter.= "//Use SQL item_db and mob_db for the map server".PHP_EOL;
		$inter.= "use_sql_db: no".PHP_EOL;
		$inter.= "".PHP_EOL;
	}
	
	// char_athena.conf
	$char .= "// Server Communication username and password.".PHP_EOL;
	$char .= "userid: ".$suname.PHP_EOL;
	$char .= "passwd: ".$spass.PHP_EOL;
	$char.= "".PHP_EOL;
	
	$char .= "// Server name, use alternative character such as ASCII 160 for spaces.".PHP_EOL;
	$char .= "server_name: ".$sname.PHP_EOL;
	$char.= "".PHP_EOL;

	// map_athena.conf
	if(!$lserv){
		changeips($lip, $mip, $cip);
	}
	$map .= "// Server Communication username and password.".PHP_EOL;
	$map .= "userid: ".$suname.PHP_EOL;
	$map .= "passwd: ".$spass.PHP_EOL;
	$map.= "".PHP_EOL;

	$readme .= "Extract the contents of the folder to your server's root folder";
	
	echo (json_encode(array($login, $inter, $char, $map, $readme)));
}

function changeips($lip, $mip, $cip){
	/* $out = file_get_contents('src/map_athena.conf');
	$out = str_replace("//char_ip: 127.0.0.1", "char_ip: ".$cip, $out);
	$out = str_replace("//map_ip: 127.0.0.1", "map_ip: ".$cip, $out); */
	//echo $txt1; 
	global $map;
	$map .= "char_ip: ".$cip.PHP_EOL;
	$map .= "map_ip: ".$mip.PHP_EOL;
	$map.= "".PHP_EOL;
}

function changeprts($lport, $mport, $cport){
	global $map, $login, $char;

	$map .= "char_port: ".$cport.PHP_EOL;
	$map .= "map_port: ".$mport.PHP_EOL;
	$map.= "".PHP_EOL;
	$login .= "login_port: ".$lport.PHP_EOL;
	$login.= "".PHP_EOL;
	$char .= "char_port: ".$cport.PHP_EOL;
	$char .= "login_port: ".$lport.PHP_EOL;
	$char.= "".PHP_EOL;
}

function checkport($host, $port)
{
	$out='';
	$connection = @fsockopen($host, $port);

	if (is_resource($connection))
	{
		$out.= $host . ':' . $port . ' ' . '(' . getservbyport($port, 'tcp') . ') is in use.' . "<br/>";
		fclose($connection);
	}

	else
	{
		$out.= $host . ':' . $port . ' is available or not reachable.' . "<br/>";
	}
	return $out;
}
?>